﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace ExplorerPlugin
{
    public partial class FileBrowser
    {
        #region Constructor


        #endregion

        #region Private Attributes

        #endregion

        #region Public Attributes

        public static ListviewButton[] tempLvButtons;
        public static string currentPath;
        public static float zoom;
        public static int padding;

        #endregion

        public ListView listView;

        public void GetDrives(ref TreeView parent, ref ListView lv, string mainNodeName)
        {
            parent.Nodes.Add(new TreeNode(mainNodeName));

            listView = lv;

            TreeNodeCollection drives = parent.Nodes[0].Nodes;

            string[] drivesArray = Environment.GetLogicalDrives();

            foreach (string s in drivesArray)
            {
                string sx = s.Split(new string[] { "\\" }, StringSplitOptions.None)[0];
                try
                {
                    if (Directory.GetDirectories(s).Length != 0)
                        drives.Add(sx).Nodes.Add("nix");
                }
                catch (IOException)
                {
                    drives.Add(sx);
                }
                drives[drives.Count - 1].Name = sx;
            }
        }

        public TreeNode[] GetSubfolders(TreeNode parent)
        {
            DirectoryInfo dir = new DirectoryInfo(parent.Name);

            DirectoryInfo[] subdirs = dir.GetDirectories();
            TreeNode[] retval = new TreeNode[subdirs.Length];

            for (int i = 0; i < subdirs.Length; i++)
            {
                retval[i] = new TreeNode(subdirs[i].Name);
                retval[i].Name = subdirs[i].FullName;
                try
                {
                    if (subdirs[i].GetDirectories().Length != 0)
                        retval[i].Nodes.Add("nix");
                }
                catch (UnauthorizedAccessException) { }
            }

            return retval;
        }



        public ListviewButton[] GetFiles(string map, Size size, int returnWidth, int padding)
        {
            DirectoryInfo dir;
            try { dir = new DirectoryInfo(map); }
            catch (ArgumentException) { return null; }

            DirectoryInfo[] subdirs = dir.GetDirectories();
            FileInfo[] files = dir.GetFiles();
            ListviewButton[] retval = new ListviewButton[subdirs.Length + files.Length];

            int noOfCols = returnWidth / (size.Width + padding);

            for (int i = 0; i < subdirs.Length; i++)
            {
                retval[i] = new ListviewButton();
                retval[i].Text = subdirs[i].Name;
                retval[i].Name = subdirs[i].FullName;
                retval[i].Size = size;
                retval[i].Location = new Point((i % noOfCols) * (size.Width + padding) + padding, ((int)(i / noOfCols) * (size.Height + padding)));
            }

            for (int i = subdirs.Length; i < files.Length + subdirs.Length; i++)
            {
                retval[i] = new ListviewButton();
                retval[i].Text = files[i - subdirs.Length].Name;
                retval[i].Name = files[i - subdirs.Length].FullName;
                retval[i].Size = size;
                retval[i].Location = new Point((i % noOfCols) * (size.Width + padding) + padding, ((int)(i / noOfCols) * (size.Height + padding)));
            }

            return retval;
        }


    }
}
